window.onload = function () {
    document.getElementById("message").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=0;i<4;i++){
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("superSail").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=4;i<6;i++){
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("order").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=6;i<a.length;i++){
            a[i].classList.toggle("hide")
        }
    }
    function searchCollection(){
        axios.get('/home/collection/collection/queryAllCollection',{
            params:{
            }
        }).then(function(data){
            if(data.data.data.code==20000){
                var list = data.data.data.data
                console.log(data)
                console.log(data.data)
                console.log(data.data.data)
                if(list.length == 0){
                    document.getElementById("myMessage").innerHTML = ``
                    document.getElementById("myMessage").innerHTML += `
                    <div class="nothing">商品收藏里什么也没有哦，赶紧去浏览添加吧</div>
                    `
                }else{
                    document.getElementById("myMessage").innerHTML = `
                    <div class="order">商品收藏</div>
                    <div class="operation">
                            <input type="checkbox" id="chooseAll" onchange="chooseAll()">全选
                            <button onclick="chooseDelete()">删除</button>
                        </div>
                    `
                    for(let i = 0;i<list.length;i++){
                        document.getElementById("myMessage").innerHTML += `
                        <div class="orderbox">
                            <div class="choose">                            
                                <input type="checkbox" class="delete" onchange="allChoose();addInDelete(${list[i].collectionId})">
                            </div>
                            <div class="orderMessage">
                                <div class="goodPhoto"><img src="${list[i].imagePath}"></div>
                                <div class="goodMessage">
                                    <div class="title">${list[i].goodsName}</div>
                                    <p class="size">${list[i].size}</p>
                                </div>
                                <div class="price">
                                    <p>￥${list[i].goodsPrice}<span>已卖${list[i].time}件</span></p>
                                </div>
                            </div>
                        </div>
                        `
                    }
                }
            }else{
                document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"收藏检索失败，请联系工作人员"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
                document.getElementById("floatContent").classList.remove("hide");
            }
        }).catch(function (error){
            console.log(error)
            document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"请检查网络情况"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
            document.getElementById("floatContent").classList.remove("hide");
        })
    }
    (function(){searchCollection()})();
}
function chooseAll(){
    var choose = document.getElementsByClassName("delete")
    var all = document.getElementById("chooseAll").checked
    if(all){
        for(let i=0;i<choose.length;i++){
            choose[i].checked = true
        }
    }else{
        for(let i=0;i<choose.length;i++){
            choose[i].checked = false
        }
    }
}
function allChoose(){
    var choose = document.getElementsByClassName("delete")
    var all = document.getElementById("chooseAll")
    var a=0
    for(i=0;i<choose.length;i++){
        if(choose[i].checked){
            a++
        }
    }
    if(a == choose.length){
        all.checked = true
    }else{
        all.checked = false
    }
}
var readyDelete = new Array();
    function addInDelete(a){
        var browseId = a
        console.log(browseId)
        if(readyDelete.indexOf(browseId)>-1){
            // console.log(readyDelete)
            // console.log(readyDelete[0])
            for(let i=0;i<readyDelete.length;i++){
                if(readyDelete[i] == browseId){
                    readyDelete.splice(i,1)
                }
            }
        }else{
            // console.log(readyDelete)
            readyDelete.push(browseId)
            readyDelete.map(Number)
            console.log(readyDelete)
        }
    }
    function chooseDelete(){
        console.log("----------------")
        console.log(readyDelete)
        console.log("----------------")
        axios.delete('/home/collection/collection/deleteSomeCollection',{
            params:{
                collectionId:readyDelete
            }
        }).then(function(data){
            if(data.data.data.code==20000){
                document.getElementById("floatContent").innerHTML ="<div>"+"删除收藏成功"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
                document.getElementById("floatContent").classList.remove("hide");
            }else{
                console.log(data.data.data)
                document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"收藏删除失败，请联系工作人员"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
                document.getElementById("floatContent").classList.remove("hide");
            }
        }).catch(function (error){
            console.log(error)
            document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"请检查网络情况"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
            document.getElementById("floatContent").classList.remove("hide");
        })
    }
function  closethis(){
    document.getElementById("floatContent").classList.add("hide");
}